package com.ygl.strategy.gateway.opentracing.tracer;

import com.ygl.strategy.gateway.tracer.DefaultGatewayStrategyTracer;
import com.ygl.strategy.opentracing.operation.StrategyOpentracingOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.server.ServerWebExchange;

import java.util.HashMap;
import java.util.Map;

/**
 * author: yanggld
 * date: 2019/12/22 18:40
 * version: 1.0
 * description: DefaultGatewayStrategyOpentracingTracer
 */
public class DefaultGatewayStrategyOpentracingTracer extends DefaultGatewayStrategyTracer {
    @Autowired
    private StrategyOpentracingOperation strategyOpentracingOperation;

    @Override
    public void trace(ServerWebExchange exchange) {
        strategyOpentracingOperation.opentracingInitialize();

        super.trace(exchange);

        strategyOpentracingOperation.opentracingHeader(getCustomizationMap());
    }

    private Map<String, String> getCustomizationMap() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "ygl");
        return map;
    }

    @Override
    public void release(ServerWebExchange exchange) {
        super.release(exchange);

        strategyOpentracingOperation.opentracingClear();
    }

    public String getTraceId() {
        return strategyOpentracingOperation.getTraceId();
    }

    public String getSpanId() {
        return strategyOpentracingOperation.getSpanId();
    }
}
